/*
 * @Author: sunzhijun 
 * @Date: 2019-10-30 15:24:01 
 * @Last Modified by: sunzhijun
 * @Last Modified time: 2020-04-21 16:52:54
 */
import CommonAction from './common';
// import React from 'react';
import { getBusinessInfo } from 'nc-lightapp-front';
import { getAppPageConfig } from 'src/hrpub/common/utils/utils';
let businessInfo = getBusinessInfo() || {};
export default class MainAction extends CommonAction {
    constructor(comp) {
        super();
        this.comp = comp;
        if (comp.props.use) {
            comp.props.use.form('dataBase', 'dataBefore', 'dataAfter', 'dataBase_add', 'dataBefore_add', 'dataAfter_add')
            comp.props.use.editTable('dataList', 'psnList', 'chgPsnList')
            comp.props.use.search('listquery')
        }
        // 设置appcode,pagecode
        this.appConfig = getAppPageConfig();
    }
    
    // 获取多语
    getLanguage = () => {
        const {props} = this.comp;
        const {
            dispatch,
            MultiInit
        } = props;
        MultiInit.getMultiLang({
            moduleId: 'sign6011',
            domainName: 'hrcm',
            callback: (json, status, init) => {
                // debugger
                dispatch({
                    type: 'main/update',
                    payload: {
                        language: json,
                        appConfig: this.appConfig
                    }
                });
                this.getTemplate();
            }
        });
    };

    // 获取模版信息
    getTemplate = () => {
        const {props} = this.comp;
        const {
            meta,
            button,
            dispatch,
            createUIDom
        } = props;
        createUIDom(this.appConfig, (res) => {
            dispatch({
                type: 'main/update',
                payload: {
                    context: res.context,
                    pageid: res.template.pageid || '',
                    titleName: res.template.name || ''
                }
            });
            this.initMeta(res.template, this, res.button);
            meta.setMeta(res.template);
            button.setButtons(res.button);
            this.getUrlParam();
        });
    };

    //初始化meta数据
    initMeta(template, opt, buttonStg) {
        // debugger
        let that = this;
        const { props, action } = this.comp;
        const { main } = props;
        let { language } = main;
        let data = template.dataList.items;
        // fixedCol(template['dataList'], ['contractnum', 'pk_majorcorp','pk_psndoc.code','pk_psndoc'],json['hrcm-000039'])
        data.push({
            attrcode: 'handleArea',
            itemtype: 'customer',
            hyperlinkflag: false,
            label: language['hrcm-000012'], /* 国际化处理： 操作*/
            width: '250px',
            textAlign: 'center',
            visible: true,
            fixed: 'right',
            render(text, record, index) {
                let data = record.values;
                let approve_state = data.approve_state.value;
                let signtype = data.signtype.value;
                let sign_state = data.sign_state.value;
                if(signtype === '2'){
                    //判断是否电子合同且状态为4时显示发起操作
                    if( sign_state === '4' && approve_state !== '-1'){
                        return (
                            <div>
                                <span style={{color: '#0073E1', cursor: 'pointer', marginRight: '12px'}}
                                    onClick={action.headerAct.aunchAction}
                                >
                                    {/* 发起 */}
                                    { language['hrcm-000043'] }
                                </span>
                                <span style={{color: '#0073E1', cursor: 'pointer'}}
                                    onClick={()=> action.headerAct.historyAction(record)}
                                >
                                    {/* 流程历史 */}
                                    { language['hrcm-000044'] }
                                </span>
                            </div>
                        )
                    }else if( sign_state  === '4'){
                        return (
                            <div>
                                <span style={{color: '#0073E1', cursor: 'pointer', marginRight: '12px'}}
                                    onClick={action.headerAct.launchAction}
                                >
                                    {/* 发起 */}
                                    { language['hrcm-000043'] }
                                </span>
                            </div>
                        )
                    }else {
                       return that.judgeRowButton(approve_state, opt, record)
                    }
                }else{
                    return that.judgeRowButton(approve_state, opt, record)
                }
            }
        });
        let isFile = buttonStg.filter(item => item.key === 'file') 
        isFile.length > 0 &&  data.push({
            attrcode: 'opr',
            itemtype: 'customer',
            hyperlinkflag: false,
            label: language['hrcm-000011'], /* 国际化处理： 附件管理*/
            width: '80px',
            textAlign: 'center',
            visible: true,
            fixed: 'right',
            render: (text, record, index) => {
                return (
                    <a style={{cursor: 'pointer'}}>
                        <i className="icon iconfont icon-fujianshenpi" onClick={() => {
                            action.headerAct.FileManagement(record);
                        }}/>
                    </a>
                )
            }
        })
    }
    // 判断行操作按钮显示
    judgeRowButton (approve_state, opt, record){
        const { action, props: { main: { language }} } = this.comp;
        if(approve_state === '-1'){ //自由态
            return (
                <div>
                    <span style={{color: '#0073E1', cursor: 'pointer',marginRight: '12px'}}
                        onClick={()=>action.headerAct.modifyAction(record)}
                    >
                        {/* 编辑 */}
                        { language['hrcm-000001'] }
                    </span>
                    <span style={{color: '#0073E1', cursor: 'pointer',marginRight: '12px'}}
                        onClick={()=>action.headerAct.deleteAction(record)}
                    >
                        {/* 删除 */}
                        { language['hrcm-000014'] }
                    </span>
                    <span style={{color: '#0073E1', cursor: 'pointer',marginRight: '12px'}}
                        onClick={()=>action.headerAct.submitAction(record)}
                    >
                        {/* 提交 */}
                        { language['hrcm-000047'] }
                    </span>
                </div>
            )
        }else if(approve_state === '2' || approve_state === '3'){ //进行中和提交态
            return (
                <div>
                    <span style={{color: '#0073E1', cursor: 'pointer',marginRight: '12px'}}
                        onClick={()=>action.headerAct.revokeAction(record)}
                    >
                        {/* 撤回 */}
                        { language['hrcm-000048'] }
                    </span>
                    <span style={{color: '#0073E1', cursor: 'pointer'}}
                        onClick={()=>action.headerAct.historyAction(record)}
                    >
                        {/* 流程历史 */}
                        { language['hrcm-000044'] }
                    </span>
                </div>
            )
        }else if(approve_state === '0' || approve_state === '1'){
            return (
                <div>
                    <span style={{color: '#0073E1', cursor: 'pointer'}}
                        onClick={()=> action.headerAct.historyAction(record)}
                    >
                        {/* 流程历史 */}
                        { language['hrcm-000044'] }
                    </span>
                </div>
            )
        }
    }
    getHeight = () => {
		let wH = window.innerHeight,
			gap = 160;
		return wH - gap;
    };
    // 初始化
    didMount = async() => {
        const { props } = this.comp;
        const { dispatch } = props;
        let H = this.getHeight();
        await dispatch({
            type: 'main/update',
            payload: {
               H: H
            }
        });
        this.getLanguage();
        // 设置左树初始值
        // this.comp.action.treeAct.customTree();
        // 设置按钮状态
        this.comp.action.btnAct.noneOrgButtonStatus();
        // 初始化高度设置
        // if (window.location.href.match('localhost:3006')) {
        //     window.location.hash = `?page=2019`;
        // }
    };

    getUrlParam = async ()=>{
        const { props, action } = this.comp;
        const { dispatch, getUrlParam, main } = props;
        let { isCardForm } = main;
        let fromApprove = getUrlParam('scene');
        if (!fromApprove && getUrlParam('status')) {
            fromApprove = 'approvesce'
        }
        
        if( fromApprove === 'approvesce' || fromApprove === 'notice'){ //来自审批中心
            let id = getUrlParam('id');
            let status = getUrlParam('status');
            await dispatch({
                type: 'main/update',
                payload: {
                    pk_org: businessInfo.groupId || '123123',
                    pk_ctrtmng: id,
                    appConfig: this.appConfig,
                    status: status,
                    fromApprove: true
                }
            });
           
            action.cardAct.getFormData();
            action.btnAct.fromApproveInitButtonStatus();
           
           
        }else{
            action.btnAct.initButtonStatus();
            if( !isCardForm ){
                dispatch({
                    type: 'main/update',
                    payload: {
                        isShowOrg: true
                    }
                })
            }
        }
    }
}